<template>
  <div class="login">
    <el-card class="card">
      <template #header>
        <h2>登录</h2>
      </template>
      <el-form label-width="60">
        <el-form-item label="用户名">
          <el-input v-model="form.username" />
        </el-form-item>
        <el-form-item label="密码">
          <el-input type="password" v-model="form.password" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="login">登录</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>
<script setup>
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import { loginApi } from '../../api/users';
import { useGlobalStore } from '../../store';

const router = useRouter();

const form = ref({
  username: '',
  password: '',
});

async function login() {
  const GlobalStore = useGlobalStore();
  const res = await loginApi({ username: form.value.username, password: form.value.password });
  GlobalStore.authorization = res.data.data;
  router.push({ name: 'UsersIndex' });
}

</script>
<style lang="scss" scoped>
.login {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  display: flex;
  justify-content: center;

  align-items: center;

  .card {
    width: 400px;
    padding: 10px;
  }
}
</style>
